package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.constant.CommonConstant;
import com.itheima.dto.InsertSetmealDTO;
import com.itheima.dto.SetmealDishesDTO;
import com.itheima.dto.UpdateSetmealDTO;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDishes;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.SetmealDishesMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import com.itheima.vo.DishVO;
import com.itheima.vo.SetmealVO;
import com.itheima.vo.SetmealVoPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
 *套餐模块业务层实现类
 * @author msi
 * @date 2022/5/17
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishesMapper setmealDishesMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public boolean insertSetmeal(InsertSetmealDTO insertSetmealDTO) {
        if (insertSetmealDTO == null) {
            return false;
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(insertSetmealDTO, setmeal);
        Category category = categoryMapper.selectById(insertSetmealDTO.getCategoryId());
        if (category == null) {
            throw new CustomException("找不到分类");
        } else {
            setmeal.setType(category.getType());
        }

        int insert = setmealMapper.insert(setmeal);

        List<SetmealDishesDTO> setmealDishesDtos = insertSetmealDTO.getSetmealDishes();

        for (SetmealDishesDTO setmealDishDTO : setmealDishesDtos) {
            SetmealDishes setmealDish = new SetmealDishes();
            BeanUtils.copyProperties(setmealDishDTO, setmealDish);
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishesMapper.insert(setmealDish);
        }

        Set<Object> keys = redisTemplate.keys(CommonConstant.SETMEAL_ID + "*");
        if(Objects.nonNull(keys)){
            redisTemplate.delete(keys);
        }
        return insert > 0;
    }

    @Override
    public SetmealVoPage getPageList(String name, Integer page, Integer pageSize) {
        IPage<Setmeal> iPage = new Page<>(page, pageSize);
        List<SetmealVO> list = new ArrayList<>();
        QueryWrapper<Setmeal> qw = new QueryWrapper<>();
        if (null == name) {
            qw = null;
        }
        if (null != name) {
            qw.like("name", name);
        }
        setmealMapper.selectPage(iPage, qw);

        List<Setmeal> records = iPage.getRecords();
        for (Setmeal setmeal : records) {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal, setmealVO);
            Category category = categoryMapper.selectById(setmeal.getCategoryId());

            if (null == category) {
                throw new CustomException("没找到套餐");
            } else {
                setmealVO.setCategoryName(category.getName());
            }

            list.add(setmealVO);
        }

        SetmealVoPage setmealVoPage = new SetmealVoPage();
        setmealVoPage.setRecords(list);
        setmealVoPage.setTotal(iPage.getTotal());
        setmealVoPage.setCurrent(page);
        setmealVoPage.setSize(pageSize);

        return setmealVoPage;
    }

    @Override
    public Boolean updateSetmeal(UpdateSetmealDTO updateSetmealDTO) {
        if (null == updateSetmealDTO) {
            return false;
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(updateSetmealDTO, setmeal);
        Category category = categoryMapper.selectById(updateSetmealDTO.getCategoryId());
        if (category == null) {
            throw new CustomException("找不到分类");
        } else {
            setmeal.setType(category.getType());
        }

        int update = setmealMapper.updateById(setmeal);


        QueryWrapper<SetmealDishes> qw = new QueryWrapper<>();
        qw.eq("setmeal_id", updateSetmealDTO.getId());

        List<SetmealDishes> setmealDishesNew = updateSetmealDTO.getSetmealDishes();

            setmealDishesMapper.delete(qw);
            for (SetmealDishes dishes : setmealDishesNew) {
                dishes.setSetmealId(updateSetmealDTO.getId());
                Dish dish = dishMapper.selectById(dishes.getDishId());
                dishes.setImage(dish.getImage());
                setmealDishesMapper.insert(dishes);
            }

        Set<Object> keys = redisTemplate.keys(CommonConstant.SETMEAL_ID + "*");
        if(Objects.nonNull(keys)){
            redisTemplate.delete(keys);
        }
        return update > 0;

    }

    @Override
    public SetmealVO getSetmeal(String id) {


        Setmeal setmeal = setmealMapper.selectById(id);
        QueryWrapper<SetmealDishes> qw = new QueryWrapper<>();
        qw.eq("setmeal_id", id);
        List<SetmealDishes> setmealDishes = setmealDishesMapper.selectList(qw);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        if (category != null) {
            setmealVO.setCategoryName(category.getName());
        }
        return setmealVO;

    }

    @Override
    public Boolean changeStatus(Integer status, String ids) {
        String[] split = ids.split(",");
        int i = 0;
        for (String id : split) {
            Setmeal setmeal = setmealMapper.selectById(id);
            setmeal.setStatus(status);
            i = setmealMapper.updateById(setmeal);
        }


        return i > 0;
    }

    @Override
    public Boolean delete(String[] ids) {
        int i = 0;
        for (String id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if (1 == setmeal.getStatus()) {
                throw new CustomException("启用套餐不可删除");
            } else {
                i = setmealMapper.deleteById(id);
                QueryWrapper<SetmealDishes> qw = new QueryWrapper<>();
                qw.eq("setmeal_id", id);
                setmealDishesMapper.delete(qw);
            }
        }

        return i > 0;
    }


    @Override
    public List<Setmeal> getList(String categoryId, Integer status) {
        String key = CommonConstant.SETMEAL_ID + categoryId + "_" + status;
        List<Setmeal> setmeals = (List<Setmeal>) redisTemplate.opsForValue().get(key);
        if (setmeals != null) {
            return setmeals;
        }

        QueryWrapper<Setmeal> qw = new QueryWrapper<>();
        if (categoryId != null) {
            qw.eq("category_id", categoryId);
        }
        if (categoryId != null) {
            qw.eq("status", status);
        }
        setmeals = setmealMapper.selectList(qw);
        redisTemplate.opsForValue().set(key, setmeals, 60, TimeUnit.MINUTES);
        return setmeals;
    }

    @Override
    public List<SetmealDishes> getSetmealDishes(String id) {
        QueryWrapper<SetmealDishes> qw = new QueryWrapper<>();
        qw.eq("setmeal_id", id);
        List<SetmealDishes> setmeals = setmealDishesMapper.selectList(qw);
        return setmeals;
    }
}
